package com.dys.mvpstudy.mvp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.dys.mvpstudy.R;
import com.dys.mvpstudy.bean.Account;
import com.dys.mvpstudy.mvp.view.IMVPView;
import com.dys.mvpstudy.presenter.MVPPresenter;

public class MVPActivity extends AppCompatActivity implements View.OnClickListener, IMVPView {

    /**
     * 展示查询结果
     */
    private TextView mTvResult;

    /**
     * 用户输入信息的输入框
     */
    private EditText mEditAccount;

    /**
     * 需要持有presenter的引用
     */
    private MVPPresenter mMvpPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvp);
        initView();
        // 对持有的Presenter进行初始化操作,因为实现了接口，所以直接传递this即可
        mMvpPresenter = new MVPPresenter(this);
    }

    /**
     * 初始化View
     */
    private void initView() {
        mTvResult = findViewById(R.id.tv_result);
        mEditAccount = findViewById(R.id.et_account);
        findViewById(R.id.btn_getAccount).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // 点击时，通知presenter,进行数据的获取，在presenter中要获取数据，就要使用model,最后通过回调的方式来通知presenter,再来通知view展示界面
        mMvpPresenter.getAccountData(getUserInfoByInput());
    }

    @Override
    public String getUserInfoByInput() {
        return mEditAccount.getText().toString();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showSuccessPage(Account account) {
        mTvResult.setText("用户账号："+account.getName() + "  |  用户等级："+account.getLevel());
    }

    @Override
    public void showFailedPage() {
        mTvResult.setText("获取数据失败!");
    }
}
